let server_url = ''; //请求地址
let url = ''; //接口
let token = '';
if (process.env.NODE_ENV === 'development') { //开发环境
	// #ifdef H5
	server_url = 'http://192.168.0.3:11118/api'
	// server_url = 'http://wsfs.jm120.net/api'
	// #endif
} else { //生产环境
	// #ifdef H5
	server_url = 'http://wsfs.jm120.net/api'
	// server_url = 'http://36.134.154.147:9091/api'
	// #endif
}

// console.log(server_url)
function service(options = {}) {
	token = uni.getStorageSync('token') || ''
	options.url = `${server_url}${options.url}`;
	//配置请求头
	options.header = {
		'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
		'authoriztion': `${token}`
	};
	return new Promise((resolved, rejected) => {
		//成功
		options.success = (res) => {
			// console.log(res)
			if (Number(res.data.code) == 200) {
				resolved(res.data)
			} else if (Number(res.data.code) == 501) {
				uni.clearStorageSync()
				uni.showModal({
					title: '提示',
					content: '当前门诊不存在！',
					showCancel: false, //没有取消按钮的弹框
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定');
						}
					}
				});
				return;
			} else {
				if (res.data.msg == 'token不合法') {
					console.log('切换医生端')
				} else {
					uni.showToast({
						icon: 'none',
						duration: 3000,
						title: `${res.data.msg}`
					});
				}
				rejected(res.data.msg)
			}
		}
		//错误
		options.fail = (err) => {
			rejected(err); //错误
		}
		uni.request(options);
	});
}
export default service;
